﻿#include"unrealircd.h"

/**
 * 获得gitlibApi访问的url
 */
char *get_gitlib_api_url(char *url){
	char *result=safe_alloc(sizeof(char )*200);
	sprintf(result,"%s%s","https://111.230.73.48/api/v4",url);
	return result;
}

/*
获得gitlib请求的head
*/
char **get_gitlib_head(int *head_num) {
	*head_num = 2;
	char **head = malloc((*head_num) * sizeof(char *));
	safe_strdup(head[0],"PRIVATE-TOKEN:PEULsK4CVWMQvxHCSAxx");
	safe_strdup(head[1],"Content-Type: application/json");
	return head;
}
/*
获得所有gitlib用户信息
*/
// void get_gitlib_all_users() {
// 	char url[200]="";
// 	strcat(url, host);
// 	strcat(url, "/users");
// 	HttpRequestData * data = malloc(sizeof(HttpRequestData));
// 	data->url = _strdup(url);
// 	data->data = _strdup("");
// 	data->userPoint = _strdup("qiu");
// 	data->requestType = 0;
// 	data->head = get_gitlib_head(&(data->head_num));
// 	data->successfinishCallFun = success_users_callfunc;
// 	data->failefinishCallFun = faile_users_callfunc;
// 	add_httpRequestReady(data);
// }

/*
注册一个用户
*/
void creat_gitlib_user(char *userName, char *name, char *password, char *email,Client *client,RegisterData *registerdata) {
	D_User *userdata=registerdata->d_user;
	cJSON * root = cJSON_CreateObject();
	cJSON_AddStringToObject(root, "username", userName);
	if (name) {
		cJSON_AddStringToObject(root, "name", name);
	}
	else {
		cJSON_AddStringToObject(root, "name", userName);
	}
	cJSON_AddStringToObject(root, "password", password);
	cJSON_AddStringToObject(root, "email", email);
	char *postdata = cJSON_PrintUnformatted(root);
	cJSON_Delete(root);
	printf("creat_gitlib_user-cjson:\n%s\n", postdata);
	HttpRequestData * data = malloc(sizeof(HttpRequestData));
	data->url = get_gitlib_api_url("/users");
	safe_strdup(data->data,postdata);
	free(postdata);
	data->userPoint = registerdata;
    data->client=client;
	data->requestType = 1;
	data->head = get_gitlib_head(&(data->head_num));
	data->successfinishCallFun = registerSuccessCallfunc;
	data->failefinishCallFun = registerFaileCallfunc;
	add_httpRequestReady(data);
}
